/*
package com.tianji.aigc.service.impl;

import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.utils.JsonUtils;
import com.tianji.aigc.config.DashScopeProperties;
import com.tianji.aigc.domain.vo.ChatEventVO;
import com.tianji.aigc.enums.ChatEventTypeEnum;
import com.tianji.aigc.service.IChatService;
import com.tianji.common.utils.TokenContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@RequiredArgsConstructor
public class AppAgentChatServiceImpl implements IChatService {

    private final DashScopeProperties dashScopeProperties;

    // 存储大模型的生成状态，这里采用ConcurrentHashMap是确保线程安全
    // 目前的版本暂时用Map实现，如果考虑分布式环境的话，可以考虑用redis来实现
    private static final Map<String, Boolean> GENERATE_STATUS = new ConcurrentHashMap<>();

    //输出结束的标记
    private static final ChatEventVO STOP_EVENT = ChatEventVO.builder()
            .eventType(ChatEventTypeEnum.STOP.getValue())
            .build();

    @Override
    public Flux<ChatEventVO> chat(String question, String sessionId) {
        // 获取对话id
        String conversationId = IChatService.getConversationId(sessionId);
        String token = TokenContext.getToken();

        var toolsMap = new HashMap<String, Object>();
        for (String tool : dashScopeProperties.getAppAgent().getTools()) {
            toolsMap.put(tool,Map.of("user_token",token));
        }
        var bizParams = Map.of("user_defined_tokens", toolsMap);

        var param = ApplicationParam.builder()
                .apiKey(dashScopeProperties.getKey())
                .appId(dashScopeProperties.getAppAgent().getId()) // 应用id
                .prompt(question)
                .incrementalOutput(true)
                .bizParams(JsonUtils.toJsonObject(bizParams))
                .sessionId(conversationId)
                .build();

        var application = new Application();

        try {
            var result = application.streamCall(param);

            // 将flowable 转化为Flux进行处理输出
            return Flux.from(result)
                    .doFirst(()->GENERATE_STATUS.put(sessionId, true))
                    .doOnComplete(()->GENERATE_STATUS.remove(sessionId))
                    .doOnError(throwable -> GENERATE_STATUS.remove(sessionId))
                    .takeWhile(s->GENERATE_STATUS.getOrDefault(sessionId, false))
                    .map(applicationResult -> {
                        // 封装大模型输出的内容
                        var text = applicationResult.getOutput().getText();
                        // 封装响应对象
                        return ChatEventVO.builder()
                                .eventType(ChatEventTypeEnum.DATA.getValue())
                                .eventData(text)
                                .build();
                    })
                    .concatWith(Flux.just(STOP_EVENT));
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void stopChat(String sessionId) {
        // 移除标记
        GENERATE_STATUS.remove(sessionId);
    }
}
*/
